package com.sxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxl.config.LayUiResult;
import com.sxl.domain.Rolemodules;
import com.sxl.domain.Roles;
import com.sxl.service.RolemodulesService;
import com.sxl.service.RolesService;
import com.sxl.service.UserrolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/4/18 9:24
 */
@RestController
@CrossOrigin
@RequestMapping("/roles")
public class RolesController {
    @Autowired
    private RolesService rolesService;
    @Autowired
    private RolemodulesService rolemodulesService;
    @Autowired
    private UserrolesService userrolesService;
    /**
     * 查询所有
     * @return返回list集合
     */
    @PostMapping("/selectAllRoles")
    public List<Roles> selectAllRoles(){
        return rolesService.list();
    }
    @GetMapping("/findAllRoles")
    public Map<String ,Object> findAllRoles(Integer page, Integer limit){
        List<Roles> list = rolesService.list();
        Page<Map<String, Object>> pageZJ = new Page<>(page, limit);
        Page<Map<String, Object>> rsl = rolesService.pageMaps(pageZJ, null);
        Map<String, Object> rslmap = new HashMap<>();
        rslmap.put("code", "0");
        rslmap.put("msg", "成功");
        rslmap.put("data", rsl.getRecords());//取数据
        rslmap.put("count", rsl.getTotal());//取总条数
        return rslmap;
    }

    /**
     * 添加角色
     * @param roles
     * @return
     */
    @PostMapping("/insertRoles")
    public Map<String ,Object> insertRoles(Roles roles){
        List<Roles> list = rolesService.list();
        for (int i=1;i<list.size();i++){
            if (roles.getName().equals(list.get(i).getName())){
                return LayUiResult.toClient("1","角色名称不能相同");
            }
        }
        boolean rel = rolesService.save(roles);
        return rel== true? LayUiResult.toClient("0", "添加成功") : LayUiResult.toClient("1", "添加失败");
    }
    /**
     * 修改角色
     * @param roles
     * @return
     */
    @PostMapping("/updateRoles")
    public Map<String ,Object> updateRoles(Roles roles){
        List<Roles> list = rolesService.list();
        Roles r = rolesService.getById(roles.getId());
        if (!roles.getName().equals(r.getName())){
            for (int i=0;i<list.size();i++){
                if(roles.getName().equals(list.get(i).getName())){
                    return LayUiResult.toClient("0", "角色名称不能相同");
                }
            }
        }
        boolean rel = rolesService.updateById(roles);
        return rel== true? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PostMapping("/deleteRoles")
    public Map<String ,Object> deleteRoles(Integer id){
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("roleId",id);
        Long count= rolemodulesService.count(wrapper);
        if (count>0){
            return LayUiResult.toClient("1","该角色下有模块，无法被删除");
        }
        Long count1= userrolesService.count(wrapper);
        if (count1>0){
            return LayUiResult.toClient("1","该角色下已被分配，无法删除");
        }
        boolean rel = rolesService.removeById(id);
        return rel== true? LayUiResult.toClient("0", "删除成功") : LayUiResult.toClient("1", "删除失败");
    }

}
